#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;


void solve()
{
    int n;
    cin >> n;
    vector<array<i64, 2>> a(n + 1);
    i64 ans = 0;
    for (int i = 1;i <= n;i++) {
        cin >> a[i][0] >> a[i][1];
    }
    sort(a.begin() + 1, a.end());
    i64 l = 1, r = n;
    while (l < r) {
        auto& [a1, b1] = a[l];
        auto& [a2, b2] = a[r];
        if (b1 > b2) {
            ans += (a2 - a1) * b2;
            b1 -= b2;
            r--;
        }
        else if (b1 < b2) {
            ans += (a2 - a1) * b1;
            b2 -= b1;
            l++;
        }
        else if (b1 == b2) {
            ans += (a2 - a1) * b1;
            l++;
            r--;
        }
        if (a1 == a2) break;
    }
    cout << ans << '\n';
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}